<template>
  <div id="vipAddindex">
    <c-title :hide="false" :text="coupon?'提货券联名':'添加会员'"></c-title>
    <div class="fromBox">
      <div class="cell first">
        <div class="cellLeft">选择会员</div>
        <div class="cellRight">
          <van-field
            v-model="userId"
            placeholder="请输入会员ID"
            input-align="right"
            @blur="tapId"
          />
        </div>
      </div>
      <div class="cellP" v-if="usetInfo">会员：{{ usetInfo.nickname }}</div>
      <div class="cellError" v-else>未找到会员</div>
      <div class="cell nofirst">
        <div class="cellLeft">联名提货券</div>
        <div class="cellRight">
          <van-field
            v-model="total"
            placeholder="请输入数量"
            input-align="right"
          />
        </div>
      </div>
      <div class="cellP">可使用：￥{{ credit }}</div>
    </div>
    <div class="posbtn set-pc-style">
      <div class="btn" @click="submitTap">确认</div>
    </div>
  </div>
</template>
<script>
import { Toast } from "vant";
export default {
  data() {
    return {
      userId: "",
      total: "",
      coupon: false,
      vid: "",
      usetInfo: null,
      cvid: "",
      credit: "",
    };
  },
  activated() {
    console.log(this.$route.params);
    if (this.$route.params.coupon) {
      this.coupon = this.$route.params.coupon;
      this.fun.setWXTitle("提货券联名");
    } else {
      this.fun.setWXTitle("添加会员");
    }
    if (this.$route.params.cvid) {
      this.cvid = this.$route.params.cvid;
    }
    if (this.$route.params.vid) {
      this.userId = this.$route.params.vid;
      this.getVipUser();
    }
    if (this.$route.params.debtId) {
      this.debtId = this.$route.params.debtId;
    }
    if (this.$route.params.credit) {
      this.credit = this.$route.params.credit;
    }
  },
  methods: {
    tapId() {
      this.getVipUser();
    },
    submitTap() {
      if (this.coupon) {
        // 提货券联名：：：：转让
        this.makeOver();
      } else {
        // 申请vip
        this.vipApply();
      }
    },
    vipApply() {
      $http
        .post(
          "plugin.debt-shop.frontend.debt-member.vipBind",
          { vip_uid: this.userId, debtor_id: this.debtId ,amount: this.total},
          "加载中"
        )
        .then((response) => {
          if (response.result === 1) {
            Toast(response.msg);
            setTimeout(() => {
              this.$router.go(-1);
            }, 1000);
          } else {
            Toast(response.msg);
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    getVipUser() {
      if (!this.userId) return;
      $http
        .post("member.member.memberInfo", { uid: this.userId }, "加载中")
        .then((response) => {
          if (response.result === 1) {
            this.usetInfo = response.data;
          } else {
            Toast(response.msg);
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    makeOver() {
      $http
        .post(
          "plugin.debt-shop.frontend.debt-member.exchangeCommonCredit",
          { connection_id: this.cvid, amount: this.total },
          "加载中"
        )
        .then((response) => {
          if (response.result === 1) {
            Toast(response.msg);
            setTimeout(() => {
              this.$router.go(-1);
            }, 1000);
          } else {
            Toast(response.msg);
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
};
</script>
<style lang="scss" scoped>
#vipAddindex {
  background: #fff;
  min-height: 100vh;

  .posbtn {
    position: fixed;
    bottom: 1.31rem;
    left: 0;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;

    .btn {
      width: 19.13rem;
      height: 2.5rem;
      background-color: #f15353;
      border-radius: 1.25rem;
      font-size: 0.94rem;
      color: #fff;
      text-align: center;
      line-height: 2.5rem;
    }
  }

  .fromBox {
    padding: 0 0.94rem;

    .first {
      padding: 0.78rem 0 0.45rem 0;
    }

    .nofirst {
      padding: 0 0 0.45rem 0;
    }

    .cellP {
      text-align: left;
      font-size: 0.75rem;
      color: #666;
      margin-bottom: 1rem;
    }

    .cellError {
      text-align: left;
      font-size: 0.75rem;
      color: #f15353;
      margin-bottom: 1rem;
    }

    .cell {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .cellRight {
        flex: 1;

        .van-cell {
          padding: 0;
        }
      }

      .cellLeft {
        font-size: 0.94rem;
        color: #333;
      }
    }
  }
}
</style>
